package com.etc.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.etc.dao.IRecordsDao;
import com.etc.dao.impl.RecordsDaoImpl;
import com.etc.entity.Records;
import com.etc.entity.Statistics;
import com.etc.service.IRecordsService;
import com.etc.util.PageData;

public class RecordsServiceImpl implements IRecordsService {
	IRecordsDao dao=new RecordsDaoImpl();
	RecordsDaoImpl recordsDao=new RecordsDaoImpl();
	private String beginTime = "09:00:00";
	private String endTime = "18:00:00";
	private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
	/**
	 * 添加员工上班打卡记录
	 * empId:员工id
	 * details:打卡详情:正常/早退/迟到
	 * return true/false
	 */
	@Override
	public boolean clockIn(int empId) {
		int details;
		Calendar c = Calendar.getInstance();
		int hour = c.get(Calendar.HOUR_OF_DAY);
		int minute = c.get(Calendar.MINUTE);
		int second = c.get(Calendar.SECOND);
		String hour_minute_second = hour + ":" + minute + ":" + second;
		Date parse = null;
		Date now = null;
		try {
			parse = sdf.parse(beginTime);
			now = sdf.parse(hour_minute_second);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long m = (now.getTime() - parse.getTime()) / 60 / 1000;
		if(m>60) {
			details = 3;//旷工
		}else if(m > 0){
			details = 1;//迟到
		}else {
			details = 0;//正常
		}
		return recordsDao.addRecord(empId,details);
	}
	/**
	 * 添加员工下班打卡记录
	 * record:记录对象
	 * return true/false
	 */
	@Override
	public boolean clockOut(Records record) {
		Calendar c = Calendar.getInstance();
		int hour = c.get(Calendar.HOUR_OF_DAY);
		int minute = c.get(Calendar.MINUTE);
		int second = c.get(Calendar.SECOND);
		String hour_minute_second = hour + ":" + minute + ":" + second;
		Date parse = null;
		Date now = null;
		try {
			parse = sdf.parse(endTime);
			now = sdf.parse(hour_minute_second);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long m = (now.getTime() - parse.getTime()) / 60 / 1000;
		if(record.getClockInDetails() != 3) {
			if(m>0) {
				record.setClockInDetails(0);//正常
			}else {
				record.setClockInDetails(2);//早退
			}
		}
		return recordsDao.updateRecord(record);
	}
	
	/**
	 * 获得当天员工的打卡记录
	 * empId:员工id
	 * return:记录对象
	 */
	@Override
	public Records getCurrentDayRecord(int empId) {
		return recordsDao.queryCurrentDayRecordById(empId);
	}
	/**
	 * 添加考勤记录(补卡)
	 * records:记录对象
	 * return true/false
	 */
	@Override
	public boolean addRecords(Records record, int empId) {
		// TODO Auto-generated method stub
		return dao.addRecords(record, empId);
	}
	/**
	 * 通过id和时间查员工考勤记录详情信息
	 * records:记录对象
	 * return List/集合
	 */
	@Override
	public List<Records> getqueryById(int empId, String beginTime, String endTime,int clockIn)  {
		// TODO Auto-generated method stub
		return dao.queryById(empId, beginTime, endTime,clockIn);
	}
	/**
	 * 查询所有员工考勤记录简略信息
	 * Statistics:记录对象
	 * return List/集合
	 */
	/*@Override
	public List<Statistics> getqueryAllRecords() {
		List<Statistics> list = dao.queryAllRecords();
		for (int i = 0; i < list.size(); i++) {
			Statistics e = list.get(i);
			int id = e.getStaId();
			if (i + 1 == list.size()) {
				break;
			}
			Statistics next = list.get(i + 1);
			int nextid = next.getStaId();
			if (id == nextid) {
				e.setNormalDays(e.getNormalDays() + next.getNormalDays());
				e.setLateDays(e.getLateDays() + next.getLateDays());
				e.setEarlyDepdays(e.getEarlyDepdays() + next.getEarlyDepdays());
				e.setLeaveDays(e.getLeaveDays() + next.getLeaveDays());
				list.remove(next);
				i--;
			}
		}

		return list;
	}*/
	/**
	 * 通过id删除考勤记录
	 * records:记录对象
	 * return true/false
	 */
	@Override
	public boolean delRecords(int recordId) {
		// TODO Auto-generated method stub
		return dao.delRecords(recordId);
	}
	/**
	 * 通过id修改考勤记录
	 * records:记录对象
	 * return true/false
	 */
	@Override
	public boolean updateRecords(Records record) {
		// TODO Auto-generated method stub
		return dao.updateRecords(record);
	}
	/**
	 * 分页查询  pageNo:当前页 
	 * pageSize:每页显示记录数 
	 * 查询所有员工考勤记录简略信息
	 * Statistics:记录对象
	 * return List/集合
	 */
	@Override
	public PageData queryKeyRecords(int pageNo, int pageSize,String begintime,
			String endtime,int empId,int clockIn,int deptId,String empName) {
		Statistics statistics= null;
		List<Statistics> list = dao.queryAllRecords(begintime,endtime,empId,clockIn,deptId,empName);
		List<Statistics> list1 =new  ArrayList<Statistics>();
		int totalCount=0;
		for (int i = 0; i < list.size(); i++) {
			Statistics e = list.get(i);
			int id = e.getStaId();
			if (i + 1 == list.size()) {
				break;
			}
			Statistics next = list.get(i + 1);
			int nextid = next.getStaId();
			if (id == nextid) {
				e.setNormalDays(e.getNormalDays() + next.getNormalDays());
				e.setLateDays(e.getLateDays() + next.getLateDays());
				e.setEarlyDepdays(e.getEarlyDepdays() + next.getEarlyDepdays());
				e.setLeaveDays(e.getLeaveDays() + next.getLeaveDays());
				list.remove(next);
				i--;
			}
			totalCount=list.size();						
		}
		int k=pageNo*pageSize;
		int u=list.size();
		if (k < u) {
			u=k;
		}
		for (int i = (pageNo-1)*pageSize; i < u; i++) {
			Statistics stati= list.get(i);
			statistics = new Statistics(stati.getStaId(), stati.getNormalDays(),stati.getLateDays(), stati.getEarlyDepdays(), stati.getLeaveDays(),null);				
			list1.add(statistics);
		}	
		PageData PageData=new PageData(list1,pageNo,pageSize,totalCount);
		return PageData;
	}
	
	/**
	 * 查询员工自身当月的打卡记录
	 * empId:员工id
	 * return:打卡记录集合
	 */
	@Override
	public List<Records> getqueryCurrentMonthRcordsById(int empId) {
		return dao.queryCurrentMonthRcordsById(empId);
	}
	
	/**
	 * 分页的方法
	 */
	@Override
	public PageData showAllRecordsByPage(int pageNo, int pageSize) {
		return recordsDao.showAllRecordsByPage(pageNo, pageSize);
	}

	@Override
	public int countAllFullAttend(int clockInDetails) {
		return recordsDao.countAllFullAttend(clockInDetails);
	}
	
	@Override
	public List<Records> showAllRecords() {
		return recordsDao.showAllRecords();
	}

	
}
